﻿using System;
using System.Diagnostics.Contracts;
using System.Text;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal class TableStylesRecord : WorkbookLevelRecord
    {
        public TableStylesRecord(string tableStyle, string  pivotStyle)
        {
            this._tableStyle = tableStyle;
            this._pivotStyle = pivotStyle;
            _cts = 144;
        }
        private FrtHeader _frtHeader = new FrtHeader() { _rt = 0x088E };
        private uint _cts;
        private string _tableStyle;
        private string _pivotStyle;

        public TableStylesRecord()
        {
        }

        protected override void ReadContent(BiffReaderContext context, int length)
        {
#if DEBUG
            var position = context.BaseStream.Position;
#endif

            this._frtHeader = new FrtHeader();
            _frtHeader.Read(context.Reader);
            Contract.Assert(_frtHeader._rt == 0x088E);
            this._cts = context.ReadUInt32();
            var cchDefTableStyle = context.ReadUInt16();
            var cchDefPivotStyle = context.ReadUInt16();
            this._tableStyle = Encoding.Unicode.GetString(context.ReadBytes(cchDefTableStyle * 2));
            this._pivotStyle = Encoding.Unicode.GetString(context.ReadBytes(cchDefPivotStyle * 2));
#if DEBUG
            Contract.Assert(context.BaseStream.Position - position == length);
#endif
 
        }

        internal override BiffRecordType RecordType
        {
            get { return BiffRecordType.TableStyles; }
        }

        internal override void WriteContent(BiffWriterWorkbookContext context)
        {
            _frtHeader.Write(context);
            Contract.Assert(_frtHeader._rt == 0x088E);
            context.Write(_cts);
            context.Write(((ushort)this._tableStyle.Length));
            context.Write((ushort)this._pivotStyle.Length);
            byte[] bytes = Encoding.Unicode.GetBytes(_tableStyle);
            context.Write(bytes);
            context.Write(Encoding.Unicode.GetBytes(_pivotStyle));
        }

        public override void Apply(BiffReaderWorkbookContext context)
        {
        }
    }
}